$(function () {
    industry_js.init();
})
/**
 * 行业js
 */
var industry_js = {
    /**
     * 初始化
     */
    init: function () {
        industry_js.initIndustrZtree();
        industry_js.loadIndustry();
    },
    /**
     * 树对象
     */
    zTree: null,
    /**
     * 当前加载的树形节点
     */
    industryNodes: null,
    /**
     * 模块树的初始化数据
     */
    zNodes: null,
    /**
     * 表格对象
     */
    Table_Obj: null,
    /**
     * 模块树形属性设置 创建人：lmz 创建时间：2017/03/29
     *
     * @type {{view: {dblClickExpand: boolean, showLine: boolean, selectedMulti:
	 *       boolean}, data: {key: {url: string}, simpleData: {enable: boolean,
	 *       idKey: string, pIdKey: string, rootPId: string}}, callback:
	 *       {onClick: Function}}}
     */
    setting: {
        view: {
            dblClickExpand: true,
            showLine: false,
            selectedMulti: false,
            // showIcon: false
        },
        data: {
            key: {
                name: "indName",
                url: "noUrl"
            },
            simpleData: {
                enable: true,
                idKey: "uuid",
                pIdKey: "puuid",
                rootPId: ""
            }
        },
        callback: {
            onClick: function (event, treeId, treeNode, clickFlag) {
                industry_js.industryNodes = treeNode;
                $("#search_words").val("");
                industry_js.searchThis();
            }
        }
    },
    /**
     * 加载模块树
     */
    initIndustrZtree: function () {
        var t = $("#industryTree");
        $.ajax({
            url: '/crm/industry/ztree',
            type: 'post',
            data: {},
            dataType: 'JSON',
            async: false,
            success: function (data) {
                industry_js.zNodes = data;

                t = $.fn.zTree.init(t, industry_js.setting, industry_js.zNodes);

                industry_js.zTree = $.fn.zTree.getZTreeObj("industryTree");

                var sel_id = "0";

                if (industry_js.industryNodes) {
                    sel_id = industry_js.industryNodes.id;
                }
                industry_js.industryNodes = industry_js.zTree.getNodeByParam(
                    "id", sel_id);
                industry_js.zTree.selectNode(industry_js.industryNodes);
                // 展开所有节点
                t.expandAll(true);
                // zTree.expandNode(industryNodes, true);

            }
        });
    },
    /**
     * 初始加载
     */
    loadIndustry: function () {
        $.ajax({
            url: "/crm/industry/allinfos", // 请求的url地址
            dataType: "json", // 返回格式为json
            async: false,// 请求是否异步，默认为异步，这也是ajax重要特性
            data: {
                pageIndex: 1,
                pageSize: 20,
                puuid: 0,
                indName: ""
            }, // 参数值
            type: "post", // 请求方式
            success: function (data) {

                industry_js.init_table(data.rows);
                $("#total_records").text(data.total);
                $("#this_page").text(1);
                $("#total_page").text(data.totalPages);
                var url = "/crm/industry/allinfos";
                setCommon({puuid: 0, indName: ""}, industry_js.Table_Obj, url);
            },
            error: function () {
            }
        })
    },
    /**
     * 初始化表格
     */
    init_table: function (datas) {
        var esay = $("#TableBox").easyTable({
            data: datas, // 初始数据，动态添加可以通过setData
            hideCheckbox: false, // 否显示复选框，获取复选框选择的数据用方法getCheckedItem
            rowClick: function (data) {// 行点击回调，参数为改行数据

                //industry_js.editInfo(data);

            },
            rowDoubleClick: function (data) {// 行双击回调，参数为改行数据
                industry_js.showInfo(data.uuid)
                // alert(JSON.stringify(data))

            },
            columns: [

              /*  {// 表格结构配置
                    title: "行业序号",// 列title文字
                    field: "uuid",// 该列对应数据哪个字段
                    width: "30%"// 列宽度设置,不设也没什么
                },*/

                {
                    title: "行业名称",// 列title文字
                    field: "indName",// 该列对应数据哪个字段
                    width: "30%"// 列宽度设置,不设也没什么
                }, {
                    title: "行业业备注",
                    field: "indRemark",
                    width: "30%",
                    render: function (data) {
                        if (data.indRemark == null) {
                            return "";
                        } else {
                            return data.indRemark;
                        }
                    }
                },
                {
                    title: "操作",
                    // width : "20%",
                    render: function (data) {

                        return industry_js.loadOperater(data);
                    }
                }]
        });
        industry_js.Table_Obj = esay;
    },
    /**
     * 加载方法
     *
     * @param data
     * @returns {String}
     */
    loadOperater: function (data) {
        var html = "";
        html += '<a id="1" onclick="industry_js.showInfo(\'' + data.uuid + '\')">查看</a>';
        html += '<a id="0" class="ml10" onclick="industry_js.editInfo(\'' + data.uuid + '\')">编辑</a>';
        html += '<a class="ml10" onclick="industry_js.deleteModuleByUuid(\'' + data.uuid + '\')">删除</a>';
        return html;
    },
    /**
     * 检索
     */
    searchThis: function () {
        var pageSize = $("#page_size").val();
        var pageIndex = 1;
        var obj = {};
        obj.pageSize = pageSize;
        obj.pageIndex = pageIndex;
        var indName = $("#search_words").val();
        var othr = {};
        othr.indName = indName;
        othr.puuid = industry_js.industryNodes.uuid;
        reset_serach(othr, obj);
    },
    /**
     * 新增行业
     */
    add: function () {
        var html = '';
        html += '<div class="ued_w">';
        html += '<dl class="clearfix">';
        html += '<dt>行业名称：</dt>';
        html += '<dd>';
        html += '<input type="text" class="text" placeholder="" id="indId" value="" style="display: none;">';
        html += '<input type="text" class="text" placeholder="" id="indName">';
        html += '</dd>';
        html += '</dl>';
        html += '<dl class="clearfix">';
        html += '<dt>行业备注：</dt>';
        html += '<dd>';
        html += '<input type="text" class="text" placeholder="" id="indRemark">';
        html += '</dd>';
        html += '</dl>';
        html += '</div>';

        var title = "新增行业";
        //var content = '<div style="padding: 20px 80px 20px;">是否在？</div>';
        //layer.alert(content);
        var url = "/crm/industry/addIndustry";
        AlertBox.confirm(html, title, industry_js.addSave, url);
    },

    /**
     * 新增保存
     */
    addSave: function (url) {
        var obj = {};
        obj.puuid = industry_js.industryNodes.uuid;
        var id = $("#indId").val();
        if (id != "") {
            obj.id = id;
            obj.uuid = $("#induuid").val();
        }
        obj.indName = $("#indName").val();
        //obj.indName = $("#indName").val();
        //obj.moduleType = $("input[name='moduleType']:checked").val();
        //obj.icon = $("#icon").val();
        obj.indRemark = $("#indRemark").val();
        $.ajax({
            url: url, // 请求的url地址
            dataType: "json", // 返回格式为json
            async: false,// 请求是否异步，默认为异步，这也是ajax重要特性
            data: obj, // 参数值
            type: "post", // 请求方式
            success: function (data) {
                AlertBox.alert(data.info, "");
                if (data.status) {
                    industry_js.searchThis();
                    industry_js.initIndustrZtree();
                }
            },
            error: function () {
            }
        })
    },
    /**
     * 查看信息
     */
    showInfo: function (uuid) {
        var obj = industry_js.Table_Obj.getData();
        var data = {};
        $.each(obj, function (i, ite) {
            if (ite.uuid == uuid) {
                data = ite;
                return false;
            }
        });
        var html = '';
        html += '<div class="ued_w">';
        html += '<dl class="clearfix">';
        html += '<dt>行业名称：</dt>';
        html += '<dd>';
        html += '<input type="text" class="text" placeholder="" id="indName" readonly="readonly" value="' + data.indName + '">';
        html += '</dd>';
        html += '</dl>';

        html += '<dl class="clearfix">';
        html += '<dt>行业备注：</dt>';
        html += '<dd>';
        var indRemark = data.indRemark == null ? "" : data.indRemark;
        html += '<input type="text" class="text" placeholder="" readonly="readonly" id="indRemark" value="' + indRemark + '">';
        html += '</dd>';
        html += '</dl>';
        html += '</div>';
        var title = "查看行业";
        //var content = '<div style="padding: 20px 80px 20px;">设置后不可恢复，只能重新设置，请确认操作？</div>';
        layer.open({
            title: title
            , content: html

        })
        //AlertBox.confirm(html, title );
    },
    /**
     * 编辑保存
     * @param id
     */
    editInfo: function (uuid) {

        var obj = industry_js.Table_Obj.getData();
        var data = {};
        $.each(obj, function (i, ite) {
            if (ite.uuid == uuid) {
                data = ite;
                return false;
            }
        });
        var html = '';
        html += '<div class="ued_w">';
        html += '<dl class="clearfix">';
        html += '<dt>行业名称：</dt>';
        html += '<dd>';
        html += '<input type="text" class="text" placeholder="" id="indId" value="' + data.id + '" style="display: none;">';
        html += '<input type="text" class="text" placeholder="" id="induuid" value="' + data.uuid + '" style="display: none;">';
        html += '<input type="text" class="text" placeholder="" id="indName" value="' + data.indName + '">';
        html += '</dd>';
        html += '</dl>';

        html += '<dl class="clearfix">';
        html += '<dt>行业备注：</dt>';
        html += '<dd>';
        var indRemark = data.indRemark == null ? "" : data.indRemark;
        html += '<input type="text" class="text" placeholder="" id="indRemark" value="' + indRemark + '">';
        html += '</dd>';
        html += '</dl>';
        html += '</div>';

        var title = "编辑行业";
        var content = '<div style="padding: 20px 80px 20px;">设置后不可恢复，只能重新设置，请确认操作？</div>';

        layer.confirm(content, function (index) {
            //do something
            var url = "/crm/industry/addIndustry";
            AlertBox.confirm(html, title, industry_js.addSave, url);
            layer.close(index);
        });

        //$("input[name='moduleType'][value='" + data.moduleType + "']").attr("checked", true);

    },
    /**
     * 批量删除
     */
    batchDeleteModuleByUuidss: function () {

        var datas = Table_Obj.getCheckedItem();
        if (datas.length <= 0) {
            AlertBox.alert('请选择有效数据', '提示');
            return;
        }
        var arr = new Array();
        $.each(datas, function (key, val) {
            arr[key] = val.uuid;
        });
        var ids = arr.join(",");
        industry_js.deleteModuleByUuid(ids);
    },
    /**
     * 删除
     * @param id
     */
    deleteModuleByUuid: function (uuid) {
        var title = "确认删除";

        layer.confirm(title, function (index) {
            industry_js.sureDeleteModuleByUuIds(uuid);
            layer.close(index);
        });
        /*  AlertBox.confirm('删除后不能恢复，请慎重！', title, industry_js.sureDeleteModuleByUuIds, uuid);*/
    },

    /**
     * 根据主键批量、单一删除
     * @param uuids
     */
    sureDeleteModuleByUuIds: function (uuids) {
        $.ajax({
            url: "/crm/industry/delIndustry",
            dataType: "json",
            type: "post",
            data: {uuids: uuids},
            success: function (data) {
                AlertBox.alert(data.info, '消息');
                if (data.status) {
                    industry_js.initIndustrZtree();
                    industry_js.searchThis();
                }
            },
            error: function (result, status) {
                $box.promptBox("系统异常，请联系管理员！");
            }
        });
    }
}
